package com.study.springmvc.v9mvc.controller;

import com.study.springmvc.v9mvc.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.concurrent.Callable;

@Controller
public class IndexController {

    @Autowired
    private MemberService memberService;

    @GetMapping("/syncPay")
    @ResponseBody
    public String syncPay() {
        System.out.println("1. before syncPay : " + Thread.currentThread().getName());
        String s = memberService.insert();
        System.out.println("4. after syncPay : " + Thread.currentThread().getName());
        return s;
    }

    @GetMapping("/asyncPay")
    @ResponseBody
    public String asyncPay() {
        System.out.println("1. before asyncPay : " + Thread.currentThread().getName());
        String s = memberService.save();
        System.out.println("4. after asyncPay : " + Thread.currentThread().getName());
        return s;
    }

    @GetMapping("/callablePay")
    @ResponseBody
    public Callable<String> callablePay() {

        Callable<String> callable = new Callable<String>() {
            public String call() {
                System.out.println("1. before callablePay : " + Thread.currentThread().getName());
                String s = memberService.update();
                System.out.println("4. after callablePay : " + Thread.currentThread().getName());
                return s;

            }
        };

        return callable;
    }


    @GetMapping("/index0")
    public String index0() {
        return "myIndex";
    }

    //http://localhost:8080/springmvc_war/index?token=123
    @GetMapping("/index")
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("myIndex");

        return modelAndView;
    }

    @GetMapping("/")
    @ResponseBody
    public String login() {
        return "this is login";
    }
}
